﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;
using Puzzler.Plugin.Interface;
using Puzzler.Project;
using Puzzler.Utils;
using Puzzler.Word.GUI;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Factory = Microsoft.Office.Tools.Factory;

namespace Puzzler.Word
{
    public partial class PuzzlerAddIn
    {
        private PuzzlerRibbon puzzlerRibbon;

        private void PuzzlerAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.DocumentChange += () =>
                                              {
                                                  if (puzzlerRibbon != null)
                                                    puzzlerRibbon.IsEnabled = Application.Documents.Count > 0;
                                              };
        } 

        private void puzzlerRibbon_AddPuzzleGameClick(object sender, EventArgs e)
        {
            IPuzzlePlugin plugin = PuzzleChoiceDialog.ShowPuzzleChoiceDialog();
            if (plugin != null)
            {
                plugin.AddGameContent(Application.Selection.Range, plugin.DrawPuzzleGameViews());
            }
        }

        private void puzzlerRibbon_AddPuzzleSolutionClick(object sender, EventArgs e)
        {
            IPuzzlePlugin plugin = PuzzleChoiceDialog.ShowPuzzleChoiceDialog();
            if (plugin != null)
            {
                plugin.AddGameContent(Application.Selection.Range, plugin.DrawPuzzleSolutionViews());
            }
        }

        private void puzzlerRibbon_ProjectLinkClick(object sender, EventArgs e)
        {
            if (PuzzlerProjectService.ProjectInfo != null)
            {
                if (MessageBox.Show("There is a project link to file '" + PuzzlerProjectService.ProjectInfo.Value.FileName + "'. If you change the link all images in the current document will be deleted!\nDo you want to continue?", "New Project Link", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                {
                    return;
                }
            }

            OpenFileDialog dlg = DialogUtils.CreateFileDialog<OpenFileDialog>();

            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                String fileName = dlg.FileName;
                FileType fileType = FileTypeUtils.FromExt(dlg.FileName);

                PuzzlerProjectService.Reset();
                PuzzlerProjectService.Load(fileName, fileType);
            }
        }

        private void PuzzlerAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            puzzlerRibbon = new PuzzlerRibbon();

            puzzlerRibbon.ProjectLinkClick += puzzlerRibbon_ProjectLinkClick;
            puzzlerRibbon.AddPuzzleGameClick += puzzlerRibbon_AddPuzzleGameClick;
            puzzlerRibbon.AddPuzzleSolutionClick += puzzlerRibbon_AddPuzzleSolutionClick;

            return puzzlerRibbon;
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(PuzzlerAddIn_Startup);
            this.Shutdown += new System.EventHandler(PuzzlerAddIn_Shutdown);
        }
        
        #endregion
    }
}
